using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TemplateWizard;
using EnvDTE;

namespace AltNet.VisualStudio
{
    public class ExtendedTemplateProperties : IWizard
    {
        #region IWizard Members

        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem) { }
        public void ProjectFinishedGenerating(EnvDTE.Project project) { }

        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {
            // Set the build action to EmbeddedResource
            projectItem.Properties.Item("BuildAction").Value = 3;
        }
        
        public void RunFinished() { }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            if (!(automationObject is DTE))
                throw new ArgumentException("automationObject is not of type DTE2");

            // The generated assembly name for the project is not able to be inserted via a built in replacement
            // we need to create our own
            DTE application = automationObject as DTE;

            // The project item that the item template is being created in could be a project or a folder
            // We will need to handle each case separately
            SelectedItem item = application.SelectedItems.Item(1); // non-zero index
            Project project = null;

            if (item.ProjectItem != null) // We are adding the item template to something like a folder
                project = item.ProjectItem.ContainingProject;
            else if (item.Project != null) // We are adding the item template to a project
                project = item.Project;

            if (project == null)
                throw new ApplicationException("Could not determine project");

            replacementsDictionary.Add("$assemblyname$", project.Properties.Item("AssemblyName").Value.ToString());
        }

        public bool ShouldAddProjectItem(string filePath) 
        { 
            return true; 
        }

        #endregion
    }
}
